package com.tofine.smarthome.ui;

import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.RadioGroup;

import com.activeandroid.query.Select;
import com.tofine.smarthome.R;
import com.tofine.smarthome.adapter.CategoryRecyclerViewAdapter;
import com.tofine.smarthome.adapter.DeviceRecyclerViewAdapter;
import com.tofine.smarthome.adapter.RoomRecyclerViewAdapter;
import com.tofine.smarthome.base.BaseActivity;
import com.tofine.smarthome.model.Category;
import com.tofine.smarthome.model.Device;
import com.tofine.smarthome.model.Room;
import com.tofine.smarthome.model.Scene;
import com.tofine.smarthome.model.SceneDevice;

import java.util.List;

import butterknife.Bind;
import info.hoang8f.android.segmented.SegmentedGroup;

/**
 * @author ddMax
 * @since 2016/5/27 20:53
 * 说明：情景详情页Activity
 */
public class SceneInfoActivity extends BaseActivity implements
        RadioGroup.OnCheckedChangeListener {
    public static final String TAG = SceneInfoActivity.class.getSimpleName();

    private List<Device> mDeviceData;
    private List<Category> mCategoryData;
    private List<Room> mRoomData;

    @Bind(R.id.segmented) SegmentedGroup segmentedGroup;
    @Bind(R.id.device_list) RecyclerView mDeviceList;
    private RecyclerView.Adapter mAdapter;
    private BottomSheetDialog dialog = null;

    private Scene mScene;

    @Override
    protected void onBeforeSetContentView() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            mScene = new Select().from(Scene.class)
                    .where("name = ?", bundle.getString("scene_name"))
                    .executeSingle();
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_scene_info;
    }

    @Override
    protected int getActionBarCustomView() {
        return R.layout.actionbar_info;
    }

    @Override
    protected boolean hasBackButton() {
        return true;
    }

    @Override
    protected void initActionBar(ActionBar actionBar) {
        super.initActionBar(actionBar);
        mTvActionBarTitle.setText(mScene.name);
    }

    @Override
    public void initView() {
        // 设置SegmentedGroup切换监听
        segmentedGroup.setOnCheckedChangeListener(this);
        getDeviceData();
        // 设置默认DeviceList为全部
        this.onCheckedChanged(null, R.id.segmented_btn_all);
    }

    /**
     * 获取当前情景中的设备数据
     */
    private void getDeviceData() {
        // 取出该情景下的所有设备
        mDeviceData = new Select().from(Device.class)
                .innerJoin(SceneDevice.class).on("devices.id = scenedevice.device")
                .where("scenedevice.scene = ?", mScene.getId())
                .execute();
        Log.d(TAG, mDeviceData.toString());
        // 取出类型和房间数据
        mCategoryData = new Select().from(Category.class).execute();
        mRoomData = new Select().from(Room.class).execute();
    }

    private void generateData() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.segmented_btn_all:
                mAdapter = new DeviceRecyclerViewAdapter(this, mDeviceData);
                break;
            case R.id.segmented_btn_category:
                mAdapter = new CategoryRecyclerViewAdapter(this, mCategoryData);
                break;
            case R.id.segmented_btn_room:
                mAdapter = new RoomRecyclerViewAdapter(this, mRoomData);
                break;
        }
        mDeviceList.setLayoutManager(new LinearLayoutManager(this));
        mDeviceList.setAdapter(mAdapter);
    }

    @Override
    protected void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        if (mAdapter instanceof CategoryRecyclerViewAdapter) {
            ((CategoryRecyclerViewAdapter) mAdapter).onSaveInstanceState(savedInstanceState);
        } else if (mAdapter instanceof RoomRecyclerViewAdapter) {
            ((RoomRecyclerViewAdapter) mAdapter).onSaveInstanceState(savedInstanceState);
        }
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (mAdapter instanceof CategoryRecyclerViewAdapter) {
            ((CategoryRecyclerViewAdapter) mAdapter).onRestoreInstanceState(savedInstanceState);
        } else if (mAdapter instanceof RoomRecyclerViewAdapter) {
            ((RoomRecyclerViewAdapter) mAdapter).onRestoreInstanceState(savedInstanceState);
        }
    }
}
